* A21a and b, A22: Ever attrition dummy ex-post

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

drop if firstt_2009==1

drop if add_sample_w2==1 | add_sample_w5==1  
gen cashcrop=0
replace cashcrop=1 if cotton_k==1|castor_k==1|groundnut_k==1

replace flyer_hyv=1 if flyer_hyv_exp==1

** creating demographic variables

gen educ_head=.
gen age_head=.
forvalues a=1/18 {
replace educ_head=a18_c`a' if a13_c`a' == 1
replace age_head=a14_c`a' if a13_c`a' == 1
replace educ_head=0 if educ_head==888
replace educ_head=0 if educ_head==15 | educ_head==16
}

replace sex=0 if sex==2
rename sex male
lab var male "=1 if male"

foreach var of varlist age_head educ_head male wealth_index hh_size {
bys id: replace `var'=`var'[_n+1] if `var'==. 
}

gsort id wave
gen w1_age_head = age_head if mkt_year==2005

gen w1_head_educ = educ_head if mkt_year==2005

gen w1_male=male if mkt_year==2005

cap drop finlit_new
egen finlit_new=rowmean(l21c l22c l23c l32c l32c l33c l34c l35c l36c l41c l42c l43c l44c)


gen w1_hasplot = hasplot if mkt_year==2005 & firstt_2009==0

gen w1_finlit = finlit_new if mkt_year==2005 & firstt_2009==0

gen w1_wealth_index = wealth_index if mkt_year==2005 & firstt_2009==0

gen w1_hh_size= hh_size if mkt_year==2005 & firstt_2009==0

gen w1_mainY_agrown = mainY_agrown if mkt_year==2005 & firstt_2009==0

gen w1_mainY_agrlabor = mainY_agrlabor if mkt_year==2005 & firstt_2009==0

gen w1_cashcrop = cashcrop if mkt_year==2005 & firstt_2009==0

gen w1_plotsize = plotsize_ha if mkt_year==2005 & firstt_2009==0

gen w1_consumption = real_consumption if mkt_year==2005 & firstt_2009==0

gen w1_cost_k_w1 = cost_k_w1 if mkt_year==2005 & firstt_2009==0

foreach x of varlist w1_cashcrop w1_mainY_agrown w1_mainY_agrlabor  ///
w1_hh_size w1_wealth_index w1_plotsize w1_male w1_head_educ w1_age_head w1_consumption  {
bys id : egen m_`x'= min(`x') 
}

replace m_w1_cashcrop=0 if m_w1_cashcrop==.

foreach x of varlist   ///
m_w1_hh_size m_w1_wealth_index  m_w1_plotsize m_w1_male m_w1_head_educ m_w1_age_head  {
gen miss_`x'=0
replace miss_`x'=1 if `x'==.
replace `x'=0 if `x'==.
}


* original treatment 

gen t1=0
replace t1=1 if originaltvillage==1

** Don't include flyer_exp in marketing assignment

local uniqinst "assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

foreach var of varlist `uniqinst' {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	}

foreach var of varlist `uniqinst' {
	replace `var' = 0 if treat_year==0
}

log using ../res/ever-attrit-ex-ante, replace
gen EP_A=0
replace EP_A=1 if bal_panel3==0

bys id : egen ever_attrit=max(EP_A)

/* treatment regressions */

local col=1
	di "`table'"
* reduced form 

	reg ever_attrit t1 firstt_2007 resurveyed year2006-year2013, r cluster(villageno)
	test t1 firstt_2007 
	estimates store cT`table'C`col++'
	predict treat_ols_v, xb
	
	reg ever_attrit `uniqinst' t1 firstt_2007 resurveyed year2006-year2013, r cluster(villageno)
	test `uniqinst' t1 firstt_2007 
	estimates store cT`table'C`col++'
	predict treat_ols_i, xb
	
	probit ever_attrit t1 firstt_2007 resurveyed year2006-year2013, r cluster(villageno)
	test t1 firstt_2007 
	estimates store cT`table'C`col++'
	predict treat_prob_v, pr
	
	probit ever_attrit `uniqinst' t1 firstt_2007 resurveyed year2006-year2013, r cluster(villageno)
	test `uniqinst' t1 firstt_2007 
	estimates store cT`table'C`col++'
	predict treat_prob_i, pr
	
	qui estout cT`table'C* using "../out/ea-everatt-treat-t3.csv", replace ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	mlabel("" "" "" "") ///
	label stats(F N)

* IV regressions

local col=1
	di "`table'"
	
	* Village IV (No Fixed Effects) 
	ivreg2 ever_attrit (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed , r cluster(villageno)
	test s_policy_units firstt_2007 
	
	estimates store cT`table'C`col++'
	predict treat_iv_v, xb 

	/* IND IV (No Fixed Effects) */
	ivreg2 ever_attrit (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed , r  cluster(villageno)
	test s_policy_units firstt_2007 
	
	estimates store cT`table'C`col++'	
	predict treat_iv_i, xb
	
	qui estout cT`table'C* using "../out/ea-everatt-treat-t3.csv", append ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	label stats(cdf N)
	
local col=1
	di "`table'"
	
	* Village IV (No Fixed Effects) 
	ivprobit ever_attrit (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed , r cluster(villageno)
	test s_policy_units firstt_2007 
	
	estimates store cT`table'C`col++'
	predict treat_ivprobit_v, pr 

	/* IND IV (No Fixed Effects) */
	ivprobit ever_attrit (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed , r  cluster(villageno)
	test s_policy_units firstt_2007 
	
	estimates store cT`table'C`col++'	
	predict treat_ivprobit_i, pr
	
	qui estout cT`table'C* using "../out/ea-everatt-treat-t3.csv", append ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	label stats(chi2 N)

/* baseline regressions */
local col=1
	di "`table'"
	
	reg ever_attrit m_w1_male m_w1_age_head m_w1_head_educ m_w1_hh_size m_w1_wealth_index m_w1_consumption m_w1_plotsize m_w1_cashcrop miss_m_w1* , r cluster(villageno)
	test m_w1_male m_w1_age_head m_w1_head_educ m_w1_hh_size m_w1_wealth_index m_w1_consumption m_w1_plotsize m_w1_cashcrop
	estimates store cT`table'C`col++'
	predict bas_attrit_ols, xb
	
	/*Probits */
	probit ever_attrit m_w1_male m_w1_age_head m_w1_head_educ m_w1_hh_size m_w1_wealth_index m_w1_consumption m_w1_plotsize m_w1_cashcrop miss_m_w1*  , r cluster(villageno)
	test m_w1_male m_w1_age_head m_w1_head_educ m_w1_hh_size m_w1_wealth_index m_w1_consumption m_w1_plotsize m_w1_cashcrop
	estimates store cT`table'C`col++'
	predict bas_attrit_probit, pr 
	
	qui estout cT`table'C* using "../out/ea-everatt-bas-t3.csv", replace ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	mlabel("" "" "" "") ///
	label stats(N)
	


foreach x of varlist treat_ols_v treat_ols_i treat_prob_v treat_prob_i treat_iv_v treat_iv_i treat_ivprobit_v treat_ivprobit_i bas_attrit_ols bas_attrit_probit {
gen inv_`x'=1/`x'
}

log close 

replace flyer_hyv=1 if flyer_hyv_exp==1

** Don't include flyer_exp in marketing assignment

local uniqinst "discount_2007 groupT muslimT hinduT video ppayT vframeT pframeT assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

foreach var of varlist `uniqinst' {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	}

foreach var of varlist `uniqinst' {
	replace `var' = 0 if treat_year==0
}

keep if bal_panel3==1

local uniqinst "discount_2007 groupT muslimT hinduT video ppayT vframeT pframeT assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

foreach var of varlist `uniqinst' {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	}

foreach var of varlist `uniqinst' {
	replace `var' = 0 if treat_year==0
}

drop if id==111246 | id==113474
local table = 1
local rastring "replace"
local outcomevars "totalrevenues_w1_real kval_w1_real fin_revenues_real totalcosts_w1_real cost_k_w1_real b110rk_w1_real b111rk_w1_real b112pk_w1_real total_mandays_k_w1 total_hired_mandays_k_w1 total_fam_mandays_k_w1 fin_costs totalprofit_w1_real profit_k_w1_real  fraction_hy fraction_cashcrop plotsize_ha"
foreach var of local outcomevars {
replace `var' = 0 if `var' == .
}

xtset id mkt_year

*a21a
foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Village IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_treat_prob_v] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* Village IV(FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_treat_prob_v] , fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	/* IND IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed  firstt_2009 [pw=inv_treat_prob_i] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* IND IV (Individual FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed firstt_2009 [pw=inv_treat_prob_i], fe cluster(villageno)
	estimates store cT`table'C`col++'	

	qui estout cT`table'C* using "../out/Ea-t3-treat-probit-invp.csv", ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}

*a21b
foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Village IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_treat_ivprobit_v] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* Village IV(FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_treat_ivprobit_v] , fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	/* IND IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed  firstt_2009 [pw=inv_treat_ivprobit_i] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* IND IV (Individual FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed firstt_2009 [pw=inv_treat_ivprobit_i], fe cluster(villageno)
	estimates store cT`table'C`col++'	

	qui estout cT`table'C* using "../out/Ea-t3-treat-ivprobit-invp.csv", ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}

*a22
foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Village IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_bas_attrit_probit] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* Village IV(FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_bas_attrit_probit] , fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	/* IND IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed  firstt_2009 [pw=inv_bas_attrit_probit] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* IND IV (Individual FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed firstt_2009 [pw=inv_bas_attrit_probit], fe cluster(villageno)
	estimates store cT`table'C`col++'	

	qui estout cT`table'C* using "../out/Ea-t3-bas-probit-invp.csv", ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}

